﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace EC2011_hk1_BT6_1041326_1041337_1041373_1041448.Utils
{
    public class DBConnector
    {
        //public static String ConnectionString = @"Data Source=TERRYBOWARD-XPS\SQLEXPRESS;Initial Catalog=PagingData;Integrated Security=True";
        public static String ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["PagingDataConnectionString"].ConnectionString;

        public static DataSet ExecuteQuery(String strSQL)
        {
            DataSet ds = new DataSet();
            SqlConnection connection = null;
            try
            {
                connection = new SqlConnection(DBConnector.ConnectionString);
                connection.Open();
                SqlCommand cmd = new SqlCommand(strSQL, connection);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(ds);
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                if(connection!=null)
                    connection.Close();
            }
            return ds;
        }

        public static int ExecuteNonQuery(String strSQL)
        {
            int affectedRow = 0;
            SqlConnection connection = null;
            try
            {
                connection = new SqlConnection(DBConnector.ConnectionString);
                connection.Open();
                SqlCommand cmd = new SqlCommand(strSQL, connection);
                affectedRow = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                return -1;
            }
            finally
            {
                if(connection!=null)
                    connection.Close();
            }
            return affectedRow;
        }
    }
}
